package com.imy.alscript;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;

import androidx.multidex.MultiDexApplication;

import com.imy.alscript.delegate.MyComponentDelegate;
import com.imy.virtual.client.core.VirtualCore;
import com.imy.virtual.client.stub.VASettings;
import com.imy.virtual.helper.utils.VLog;

import mirror.Reflection;


public class VApp extends MultiDexApplication {
    private static VApp gApp;
    private SharedPreferences mPreferences;

    public static VApp getApp() {
        return gApp;
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        mPreferences = base.getSharedPreferences("va", Context.MODE_MULTI_PROCESS);
        if (VASettings.IS_EMULATOR) {
            VASettings.ENABLE_IO_REDIRECT = false;
        } else {
            VASettings.ENABLE_IO_REDIRECT = true;
        }
        VASettings.ENABLE_INNER_SHORTCUT = false;
        //NonSdkManager.getInstance().visibleAllApi();
        try {
            Log.i("VApp", "attachBaseContext:");
            Reflection.unseal(base);
            VirtualCore.get().startup(base);

        } catch (Throwable e) {
            e.printStackTrace();
            VLog.printThrowable(e);
        }
    }

    @Override
    public void onCreate() {
        gApp = this;
        super.onCreate();
        virtualCore = VirtualCore.get();
        virtualCore.initialize(virtualInitializer);
    }

    VirtualCore virtualCore = null;
    VirtualCore.VirtualInitializer virtualInitializer = new VirtualCore.VirtualInitializer() {
        @Override
        public void onMainProcess() {

        }

        @Override
        public void onVirtualProcess() {
            //virtualCore.setComponentDelegate(new MyComponentDelegate());
            //virtualCore.setPhoneInfoDelegate(new MyPhoneInfoDelegate());
            //virtualCore.setTaskDescriptionDelegate(new MyTaskDescriptionDelegate());
        }

        @Override
        public void onServerProcess() {
            //virtualCore.setAppRequestListener(new MyAppRequestListener(VApp.this));
            //virtualCore.addVisibleOutsidePackage("com.tencent.mobileqq");
            //virtualCore.addVisibleOutsidePackage("com.tencent.mobileqqi");
            //virtualCore.addVisibleOutsidePackage("com.tencent.minihd.qq");
            //virtualCore.addVisibleOutsidePackage("com.tencent.qqlite");
            //virtualCore.addVisibleOutsidePackage("com.facebook.katana");
            //virtualCore.addVisibleOutsidePackage("com.whatsapp");
            //virtualCore.addVisibleOutsidePackage("com.tencent.mm");
            //virtualCore.addVisibleOutsidePackage("com.immomo.momo");
        }
    };

    public static SharedPreferences getPreferences() {
        return getApp().mPreferences;
    }
}
